package com.cloudwise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloudwise.entity.LayUiTree;
import com.cloudwise.entity.Question;
import com.cloudwise.entity.User;
import com.cloudwise.service.MenuService;
import com.cloudwise.service.QuestionService;
import com.cloudwise.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/11/25 15:04
 * @description：专门做页面跳转的controller
 * @modified By：
 * @version: 1.0
 */
@Controller
@Slf4j
public class ForwardController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 去美女页面
     * @create time: 2021/11/25 15:13
     * @return
     */
    @RequestMapping("test/toTestLogin")
    public String toTestLogin(){
        return "test/testLogin";
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 去后台登录页面
     * @create time: 2021/12/3 15:21
     * @return String
     */
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("question/toShowQuestion")
    public String toShowQuestion(){
        return "question/showQuestion";
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description:跳转到显示所有测试者的页面
     * @create time: 2021/11/29 15:37
     * @param
     * @return java.lang.String
     */

    @RequestMapping("tester/toShowTester")
    public String toShowTester(){
        log.debug("tester/toShowTester");
        return "tester/showTester";
    }
    @RequestMapping("log/toShowLog")
    public String toShowLog(){
        return "log/showLog";
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description:跳转到测试的页面
     * @create time: 2021/11/29 15:37
     * @param
     * @return java.lang.String
     */
    @RequestMapping("test/toTest")
    public ModelAndView toTest(Integer testerId,ModelAndView modelAndView){
        //去之前，把所有的有效题目信息带过去
       /* Question question= new Question();
        question.setStatus(0);*/
        QueryWrapper<Question> queryWrapper = new QueryWrapper();
        //过滤失效的问题
        queryWrapper.eq("status",0);
        List<Question> questionList = questionService.list(queryWrapper);
        modelAndView.addObject("questionList",questionList);
        modelAndView.addObject("testerId",testerId);
        modelAndView.setViewName("test/testQuestion");
        return modelAndView;
    }

    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 登陆成功之后去主页
     * @create time: 2021/12/3 16:00
     * @param userId
     * @return ModelAndView
     */
    @RequestMapping("toHome")
    public ModelAndView toHome(Integer userId,ModelAndView modelAndView){
        //根据用户id查询用户信息
        User user = userService.getById(userId);
        //将用户信息保存在model中
        modelAndView.addObject("user",user);
        //根据用户id查询所有的菜单信息
        List<LayUiTree> menus= menuService.queryAllMenusByUserId( userId);
        //将组装好的LayUiTree类型的menu集合保存在model中
        modelAndView.addObject("menus",menus);
        //跳转到主页
        modelAndView.setViewName("home");
        return modelAndView;

    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 注销功能
     * @create time: 2021/12/7 10:11
     * @return
     */
    @RequestMapping("logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

}
